<?php

namespace app\user\controller;
use app\common\controller\UserBase;


class Recovery extends UserBase
{
    protected function _initialize()
    {
        parent::_initialize();
        !config('website_status') && die(config('colse_explain'));
        $config = cache('db_config_data');
        if (!$config) {
            $config = [];
            foreach (model('config')->select() as $v) {
                $config[$v['group']][$v['name']] = $v['value'];
            }
            cache('db_config_data', $config);
        }
        config($config);
    }
    
    //参数设置
    public function index()
    {
        $list = model('customer')->order('delete_time desc,id desc')->where("delete_time > 0")->paginate(config('page_number'));
        return $this->fetch(is_Client('index'), ['list' => $list]);
    }
    
   //回收站恢复
    public function regain()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $strID=$param['id'];
            $regainCustomer = model('customer')->where(['id'=>$strID])->update(['delete_time'=>Null]);
            if ($regainCustomer) {
                //同时删除跟进记录
                model('customerTrack')->where(['cid'=>$strID])->update(['delete_time'=>Null]);
                //同时删除提醒
                model('customerRemind')->where(['cid'=>$strID])->update(['delete_time'=>Null]);
                //同时删除跟进记录
                model('customerOrder')->where(['cid'=>$strID])->update(['delete_time'=>Null]);
                insert_user_log('回收站中恢复了客户ID:'.$strID);
                $this->success('恢复客户成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }
     //回收站彻底删除
     public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $strID=$param['id'];
            if ($this->delete('customer', $this->request->param()) === true) {
                //同时删除跟进记录
                model('customerTrack')->where(['cid'=>$strID])->delete();
                //同时删除提醒
                model('customerRemind')->where(['cid'=>$strID])->delete();
                //同时删除跟进记录
                model('customerOrder')->where(['cid'=>$strID])->delete();
                insert_user_log('彻底删除了客户ID:'.$strID);
                $this->success('彻底删除成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }  
}
